package com.base.llpark.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by arvin on 2018/8/15.
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseHolder> {

    protected List<T> mData;
    protected Context mContext;
    protected SparseArray<Integer> mLayouts = new SparseArray<>();
    public static final int sType_normal = 0;
    public static final int sType_Header = 1;
    public static final int sType_bottom = 2;
    protected int mHasHeader;
    protected int mHasBottom;
    protected int mHeaderId;
    protected int mBottomId;
    protected boolean mNeedSquare;
    protected int mSpanCount;
    protected final LayoutInflater mLayoutInflater;

    protected OnItemClickListener mOnItemClickListener;
    protected OnItemLongClickListener mOnItemLongClickListener;


    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
    }

    public BaseAdapter(List<T> data, Context context) {
        mData = data;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }



    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int layoutId = 0;
        switch (viewType) {
            case sType_Header:
                layoutId = mHeaderId;
                break;
            case sType_bottom:
                layoutId = mBottomId;
                break;
            default:
                break;
        }
        if (layoutId == 0) {
            layoutId = mLayouts.get(viewType);
        }
        View inflate = mLayoutInflater.inflate(layoutId, null, false);
        BaseHolder holder = BaseHolder.getHolder(mContext, inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final BaseHolder holder, int position) {
        final boolean isHeader = getItemViewType(position) == sType_Header;
        final boolean isBottom = getItemViewType(position) == sType_bottom;
        final int dataPosition = position - mHasHeader;
        T data = null;
        if (dataPosition >= 0 && dataPosition <= mData.size() - 1) {
            data = mData.get(dataPosition);
        }
        convert(holder, data, isHeader, isBottom);

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder, dataPosition, isHeader, isBottom);
                }
            });
        }

        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemLongClickListener.onItemLongClick(holder, dataPosition, isHeader, isBottom);
                    return true;
                }
            });
        }
    }


    public abstract void convert(BaseHolder holder, T t, boolean isHeader, boolean isBottom);


    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size() + mHasBottom + mHasHeader;
        }
        return mHasBottom + mHasHeader;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHasHeader != 0 && position == 0) {
            return sType_Header;
        }
        if (mHasBottom != 0 && position == getItemCount() - 1) {
            return sType_bottom;
        }

        return getType(position - mHasHeader);
    }


    public int getType(int position) {
        return sType_normal;
    }


    public void addHeader(int layoutId) {
        mHasHeader = 1;
        mHeaderId = layoutId;
    }

    public void addBottom(int layoutId) {
        mHasBottom = 1;
        mBottomId = layoutId;
    }

    public void needSquare(int spanCount) {
        mNeedSquare = true;
        mSpanCount = spanCount;
    }


    public void putType(int type, int layoutId) {
        mLayouts.put(type, layoutId);
    }


    public void putNormalType(int layoutId) {
        putType(sType_normal,layoutId);
    }


    public interface OnItemClickListener {
        void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom);
    }


    public interface OnItemLongClickListener {
        void onItemLongClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom);
    }


}
